<template>
  <el-dialog
    v-model="dialogVisible"
    title="选择分组"
    :fullscreen="fit.fullscreen"
    :before-close="handleClose"
  >
    <el-select-v2
      v-model="group_ids"
      filterable
      :options="list"
      placeholder="选择分组"
      :props="{ value: 'id', label: 'name' }"
      style="width: 100%"
      clearable
      :multiple="muti"
    />
    <div class="mt-1">
      <el-button type="primary" text size="small" @click="goGourp"
        >增加分组</el-button
      >
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue";
import { $getGroup } from "@api/common";
import { $setUsersGroup, $updateUserGroup } from "@api/user";
import { useLocaleStore } from "@/store/locales";
import router from "@/router/";
const useLocale = useLocaleStore();
const list = ref([]);
const dialogVisible = ref(false);
const group_ids = ref(null);
const props = defineProps({
  muti: {
    type: Boolean,
    default: false,
  },
  euids: {
    type: Array,
    default: [],
  },
  row: {
    type: Object,
    default: {},
  },
  type: {
    type: String,
    default: "",
  },
});
if (props.type === "user") {
  group_ids.value = props.row.group_ids;
}
let fit = reactive({
  fullscreen: false,
});

if (useLocale.isMob) {
  fit.fullscreen = true;
} else {
  fit.fullscreen = false;
}

const getData = async () => {
  let res = await $getGroup();

  if (res.code == 0) {
    list.value = res.data.list;
  }
};

const selectItme = (item) => {
  //useCommon.setmSelected(item);
};

const handleClose = (done: () => void) => {
  group_ids.value = [];
  done();
};
const confirm = async () => {
  let res = null;

  switch (props.type) {
    case "user":
      let user = {
        euid: props.row.euid,
        group_ids: group_ids.value,
      };
      console.log(user);

      res = await $updateUserGroup(user);

      break;
    case "users":
      let users = {
        euids: props.euids,
        group_id: group_ids.value,
      };
      console.log(users);
      res = await $setUsersGroup(users);
      break;
  }
  if (res.code == 0) {
    window.location.reload();
    dialogVisible.value = false;
    ElMessage({
      message: "更新成功",
      type: "success",
    });
  }else{
    ElMessage.error(res.msg)
  }
};
const goGourp = () => {
  //  router.push('')
};
const changeShow = () => {
  getData();
  dialogVisible.value = true;
};


defineExpose({ changeShow });
</script>

<style lang="scss" scoped></style>
